package ttOrder.controller;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import ttOrder.model.po.User;
import ttOrder.service.UserServiceImpl;
import ttOrder.util.CookieUtil;

@Controller
@RequestMapping("/ttOrder")
public class userLoginController {
	
	@Resource
	private UserServiceImpl userService;
/*	
	//登录验证
	@ResponseBody
	@RequestMapping("/login")
	public Boolean loginUser(@RequestBody User user,Model model){
		Boolean result = false;
		if(userService.checkLogin(user)!=null){
			model.addAttribute("user",user);
			result =  true;
		}
		return result;
	}*/
	
	@RequestMapping(value="/login",method = RequestMethod.GET)
	public String toLogin(Model model) {
		model.addAttribute("LoginStatus", null);
		return "/Backstage/login";
	}
	
	//登录
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String doLogin(@Validated User vo,HttpSession session, HttpServletResponse response,Model model) throws Exception {
		if (vo != null) {
			User result = userService.getByKey(vo);
			if (result != null) {
				if (vo.getPassword() != null) {
					//String encodePassword = MD5Utils.GetMD5Code(vo.getPassword());
					String encodePassword = vo.getPassword();
					Date currentTime = null;
					Calendar currentTimecalendar = Calendar.getInstance();// 创建日历对象
					currentTimecalendar.setTime(new Date());
					currentTimecalendar.add(Calendar.MINUTE, -5);
					currentTime = currentTimecalendar.getTime();
					System.out.println(currentTime + "-------------" + new Date());
					if (result.getLastLockTime().before(currentTime)) {
						System.out.println(result.getLastLockTime());
						// 不在锁定时间范围内
						System.out.println("不在锁定时间范围内");
						System.out.println();
						if (encodePassword.equals(result.getPassword())) {
							// 登录成功时清0
							result.setLoginFailCount(0);
							userService.updateUserLoginFailCount(result);
							session.setAttribute("currentUser", result);
							CookieUtil.saveUserRoleToCookie(response, result.getUserRole());
							CookieUtil.saveUserIdToCookie(response, result.getId());
							if(result.getUserRole() == 4){
								return "redirect:/main";
							}
							else{
								return "redirect:/index";
							}
						} else {
							model.addAttribute("LoginStatus", "WrongPassword");
							result.setLoginFailCount(result.getLoginFailCount() + 1);
							if (result.getLoginFailCount() == 3) {
								result.setLoginFailCount(0);
								result.setLastLockTime(new Date());
								userService.updateUserLastLockTime(result);
							}
							userService.updateUserLoginFailCount(result);
						}
					} else {
						// 锁定时间内
						System.out.println("目前账号被锁定,无法登陆,请稍等五分钟.");
						model.addAttribute("LoginStatus", "Lock");
					}
				}
			}
			else{
				model.addAttribute("LoginStatus", "NoUser");
			}
		}
		else{
			model.addAttribute("LoginStatus", "nullId");
		}
		return "/Backstage/login";
	}
	
	/*
	@ResponseBody
	@RequestMapping(value="/logout",method = RequestMethod.POST)
	public String logout(HttpSession session){
		session.removeAttribute("user");		
		return "/login";
	}
	*/
	
	//注册和验证
	@ResponseBody
	@RequestMapping(value="/register",method = RequestMethod.POST)
	public Boolean doRegister(User user,Model model){
		Boolean result = false;
		if(userService.getByEntity(user) == null){
			userService.insert(user);
			model.addAttribute("user",user);
			result = true;
		}
		return result;
	}
	
}
